package com.hspedu.newData.dmsxl.树.二叉树的遍历.层序遍历迭代法.队列;

import com.hspedu.newData.dmsxl.ds.Node;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-23 
 * @Version: 1.0
 */

public class LC429N叉树的层序遍历 {

    ArrayList<List<Integer>> result = new ArrayList<>();

    public List<List<Integer>> levelOrder(Node root) {
        if (root == null) return result;
        Queue<Node> queue = new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()){
            ArrayList<Integer> list = new ArrayList<>();
            int len = queue.size();

            while(len > 0){
                Node node = queue.poll();

                list.add(node.val);
                List<Node> children = node.children;
                for (Node child : children) {
                    queue.offer(child);
                }

                len --;
            }
            result.add(list);
        }
        return result;
    }
}
